package com.house365.core.thirdpart.auth.qqwb;

import java.io.File;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;

import android.text.TextUtils;

import com.house365.core.constant.CorePreferences;
import com.house365.core.json.JSONObject;
import com.house365.core.thirdpart.auth.AppKeyInfo;
import com.house365.core.thirdpart.auth.Thirdpart;
import com.house365.core.thirdpart.auth.WeiboApiRequest;
import com.house365.core.thirdpart.auth.WeiboAsyncRequest.RequestListener;
import com.house365.core.thirdpart.auth.WeiboException;
import com.house365.core.thirdpart.auth.WeiboHttpAPi;
import com.house365.core.thirdpart.auth.dto.AccessToken;

/**
 * @author py
 * @date 2012-11-6
 * @comment
 */
public class QQWeiboApiRequest implements WeiboApiRequest {

	@Override
	public String post(WeiboHttpAPi httpApi, Thirdpart weibo,
			AppKeyInfo appkeyInfo, String content, String pic,
			RequestListener listener) throws WeiboException {
		String s;
		try {
			s = null;
			AccessToken accessToken = weibo.getAccessToken(Thirdpart.TYPE_QQ_WEIBO);
			if (!TextUtils.isEmpty(pic)) {
				// upload
				MultipartEntity entity = new MultipartEntity();
				entity.addPart(
						"oauth_consumer_key",
						new StringBody(appkeyInfo.getApp_key(), Charset
								.forName(HTTP.UTF_8)));
				entity.addPart(
						"access_token",
						new StringBody(accessToken.getToken(), Charset
								.forName(HTTP.UTF_8)));
				entity.addPart("openid",
						new StringBody(accessToken
								.getOpenid(), Charset.forName(HTTP.UTF_8)));
				entity.addPart(
						"oauth_version",
						new StringBody("2.a", Charset
								.forName(HTTP.UTF_8)));
				entity.addPart("scope",
						new StringBody("all", Charset.forName(HTTP.UTF_8)));
				entity.addPart(
						"format",
						new StringBody("json", Charset
								.forName(HTTP.UTF_8)));
				entity.addPart("content",
						new StringBody(content, Charset.forName(HTTP.UTF_8)));
			
				entity.addPart("pic", new FileBody(new File(pic)));
				s = httpApi.post(weibo.getUploadUrl(Thirdpart.TYPE_QQ_WEIBO), entity);
			} else {
				// update
				List<NameValuePair> oList = new ArrayList<NameValuePair>();
				// http://wiki.open.t.qq.com/index.php/API%E6%96%87%E6%A1%A3/%E5%BE%AE%E5%8D%9A%E7%9B%B8%E5%85%B3/%E5%8F%91%E8%A1%A8%E4%B8%80%E6%9D%A1%E5%BE%AE%E5%8D%9A
				oList.add(new BasicNameValuePair("oauth_consumer_key",
						appkeyInfo.getApp_key()));
				oList.add(new BasicNameValuePair("access_token", accessToken
						.getToken()));
				oList.add(new BasicNameValuePair("openid", accessToken
						.getOpenid()));
				oList.add(new BasicNameValuePair("oauth_version", "2.a"));
				oList.add(new BasicNameValuePair("scope", "all"));
				// oList.add(new BasicNameValuePair("clientip",""));
				oList.add(new BasicNameValuePair("format", "json"));
				oList.add(new BasicNameValuePair("content", content));
				s = httpApi.post(weibo.getUpdateUrl(Thirdpart.TYPE_QQ_WEIBO), oList);
			}
		} catch (Exception e) {
			CorePreferences.ERROR(e);
			throw new WeiboException("send weibo occurs exception", e);
		}
		int errCode = 0;
		String msg = "";
		int ret = 0;
		try {
			JSONObject json = new JSONObject(s);
			ret = json.getInt("ret");
			errCode = json.getInt("errcode");
			msg = json.getString("msg");
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (errCode != 0) {
			throw new WeiboException(getReadableMsg(ret, errCode), errCode);
		}

		return s;
	}

	private String getReadableMsg(int type, int errcode) {
		String error = null;
		switch (errcode) {
		case 1:
			error = "clientip错误！";
			break;
		case 2:
			error = "内容超出长度限制或为空！";
			break;
		case 4:
			error = "讲文明，树新风！";
			break;
		case 5:
			error = "禁止访问！";
			break;
		case 9:
			error = type == 1 ? "包含垃圾信息！" : "图片大小超出限制或为0！";
			break;
		case 10:
			error = type == 1 ? "频率限制！"
					: " 图片格式错误，目前仅支持gif、jpeg、jpg、png、bmp及ico格式";
			break;
		case 13:
			error = "请不要连续发表重复内容！";
			break;
		case 1472:
			error = "腾讯服务器内部错误导致发表失败！";
			break;
		case 70:
			error = "上传图片失败！";
			break;
		case 16:
			error = "腾讯服务器内部错误导致发表失败！";
			break;

		default:
			break;
		}
		return error;
	}

}
